﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using BrightIdeasSoftware;

using FCNS.KeepMoneyLibrary.Data;
using FCNS.KeepMoney.Data;
using FCNS.KeepMoney.Util;
using FCNS.KeepMoney.Configuration;

namespace FCNS.KeepMoney.Forms
{
    public partial class ImportForm : ParentForm
    {
        public ImportForm()
        {
            InitializeComponent();
            Program.Translation.ApplyTo(this);
        }

        ComboBox cb = new ComboBox();
        List<EntryField> editFields = null;
        List<GroupField> income = new List<GroupField>();
        List<GroupField> expenses = new List<GroupField>();
        Hashtable columnField = new Hashtable();
        string[] incomeStr;
        string[] expensesStr;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fields">null 为导入,否则请传入需要导出的数据</param>
        /// <returns></returns>
        public bool Init(List<EntryField> fields)
        {
            if (fields == null)
            {
               // tabControl1.TabPages.RemoveAt(2);
                this.Text = LanguageUtil.数据导入;
                FCNS.Utility.BannerFactory.CreateBannerEx(this, pictureBox1, Properties.Resources.import_32,
                    LanguageUtil.数据导入导出, LanguageUtil.支持多种格式的数据导入导出);

                foreach (BankField field in BankField.GetBankField.FindAll(bf => { return bf.Enable; }))
                {
                    comboBoxPurse.Items.Add(field.Name);
                    if (field.Default)
                        comboBoxPurse.SelectedIndex = comboBoxPurse.Items.Count - 1;
                }

                List<string> i = new List<string>();
                List<string> o = new List<string>();
                foreach (GroupField gf in GroupField.GetGroupField)
                {
                    if (gf.IorE)
                    {
                        income.Add(gf);
                        i.Add(gf.Name);
                    }
                    else
                    {
                        expenses.Add(gf);
                        o.Add(gf.Name);
                    }
                }
                incomeStr = i.ToArray();
                expensesStr = o.ToArray();

                cb.DropDownStyle = ComboBoxStyle.DropDownList;
                checkedListBoxField.Enabled = false;
            }
            else
            {
                tabControl1.TabPages.RemoveAt(1);
                this.Text = LanguageUtil.数据导出;
                FCNS.Utility.BannerFactory.CreateBannerEx(this, pictureBox1,
                    Properties.Resources.export_32, LanguageUtil.数据导入导出, LanguageUtil.支持多种格式的数据导入导出);

                buttonOk.Text = LanguageUtil.导出;
                editFields = fields;
                groupBoxPurse.Enabled = false;

                //columnField["columnName"] = "摘要";
                //columnField["columnDate"] = "日期";
                //columnField["columnMoney"] = "金额";
                //columnField["columnNote"] = "备注";
                //columnField["columnTag"] = "标记";
                //columnField["columnAlertDate"] = "提醒";
                //columnField["columnBankName"] = "钱袋";
                //columnField["columnGroupName"] = "群组";
                //columnField["Id"] = "Id";
                //columnField["GroupId"] = "钱袋Id";
                //columnField["BankId"] = "群组Id";
                
            }

            foreach (IDataFormat f in Program.KMDataFormat)
                listBoxFormats.Items.Add(f.Name);

            listBoxFormats.SelectedIndex = 0;

            return true;
        }

        private void OnBrowserFile(object sender, EventArgs e)
        {
            string filter = Program.KMDataFormat[listBoxFormats.SelectedIndex].Extension;
            if (editFields==null)
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Filter = filter;
                    if (ofd.ShowDialog() == DialogResult.OK)
                        textBoxPath.Text = ofd.FileName;

                }
            }
            else
            {
                using (SaveFileDialog sfd = new SaveFileDialog())
                {
                    sfd.Filter = filter;
                    if (sfd.ShowDialog() == DialogResult.OK)
                        textBoxPath.Text = sfd.FileName;
                }
            }
        }

        private void OnImportOrExportFile(object sender, EventArgs e)
        {
            if (textBoxPath.Text.Length < 2)
                return;

            if (tabControl1.SelectedIndex==0 && editFields==null)
                ImportFile();
            else
                Save();

            tabControl1.SelectedIndex = 1;
        }

        private void ImportFile()
        {
            dataListView1.ClearObjects();
          dataListView1.SetObjects(Program.KMDataFormat[listBoxFormats.SelectedIndex].ImportFile(textBoxPath.Text));
        }

        private void ExportFile()
        {
            Program.KMDataFormat[listBoxFormats.SelectedIndex].ExportFile(editFields, textBoxPath.Text);
        }

        private void OnSubmitDataFormat(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AppDefines.HelpTopic.Show(AppDefines.HelpTopic.NoFormat);
        }

        private void OnTabControlSelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                if (editFields == null)
                    buttonOk.Text = LanguageUtil.数据导入;
                else
                    buttonOk.Text = LanguageUtil.数据导出;
            }
            else
                buttonOk.Text =LanguageUtil.保存;
        }

        private void Save()
        {
            if (editFields != null)
                ExportFile();
            else
            {
                editFields = (List<EntryField>)dataListView1.Objects;
                foreach (EntryField ef in editFields)
                {
                    ef.BankId = BankField.GetBankField[comboBoxPurse.SelectedIndex].Id;
                    ef.AddRowToDataTable();
                }
                Program.SaveKMD = true;
                Program.TheMainForm.ReLoadEntry();
            }
            MessageBox.Show(LanguageUtil.完成);
            this.Close();
        }

        private void OnEditCellStart(object sender, CellEditEventArgs e)
        {
            cb.Items.Clear();
            if (((EntryField)dataListView1.SelectedObject).Money>0)
                cb.Items.AddRange(incomeStr);
            else
                cb.Items.AddRange(expensesStr);

            cb.Bounds = e.CellBounds;
            e.Control = cb;
            cb.Text = e.Value.ToString();
        }

        private void OnEditCellFinish(object sender, CellEditEventArgs e)
        {
            EntryField ef = (EntryField)dataListView1.SelectedObject;
            if (ef.Money>0)
                ef.GroupId = income[cb.SelectedIndex].Id;
            else
                ef.GroupId = expenses[cb.SelectedIndex].Id;
        }

        private void OnDownLoadExample(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Program.KMDataFormat[listBoxFormats.SelectedIndex].ExampleUrl);
        }
    }
}
